﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyLibrary.API.Web;
using MyLibrary.API.Ext;
using FifaChamps.API.Logic;
using FifaChamps.WebUI.Security;

namespace FifaChamps.WebUI
{
    /// <summary>
    /// Summary description for Proxy
    /// </summary>
    public class Proxy : MyHandler
    {
        const string LOGIN_SUCCESS = LoginManager.SUCCESS_URL;

        protected override void ProcessCommand(string cmd)
        {
            try
            {
                switch (cmd)
                {
                    case "get_players":
                        Get_Players();
                        break;
                    case "save_player":
                        Save_UpdatePlayer();
                        break;
                    case "login_user":
                        Login_User();
                        break;
                    case "get_tournaments":
                        Get_Tournaments();
                        break;
                }
            }
            catch (Exception error)
            {
                Failure(error.ToString());
            }
        }

        private void Get_Tournaments()
        {
            MenuItem rootItem = new MenuItem("Tournaments");            
            #region get the tournaments and render them and there stages
            var tornies = Get_TournamentsLocal();
            foreach (var torny in tornies)
            {
                var parameters = new Dictionary<string, object>();
                parameters.Add("ID", torny.ID);
                parameters.Add("Name", torny.Name);                
                MenuItem newItem = new MenuItem(torny.Name, new LoadTab("tournament" + torny.ID.ToString(), "fifachamps.tournament.load_tournament", parameters));
                rootItem.AddChild(newItem);
            }
            #endregion

            Flush(MenuItem.WriteMenu(rootItem, true));
        }

        private void Login_User()
        {
            var username = Request["username"];
            var password = Request["password"];

            var token = LoginManager.Get().Login(username, password);
            if (token != null)
            {
                var logged_token = LoginManager.Get().Get_LoggedIn();

                if (logged_token != null && token.ID.ToString().ToUpper().Equals(logged_token.ID.ToString().ToUpper()))
                {
                    Success(LOGIN_SUCCESS);
                    return;
                }
            }

            Failure("Invalid Login");
            return;
        }

        private void Get_Players()
        {
            int start = Convert.ToInt32(Request["start"]);
            int limit = Convert.ToInt32(Request["limit"]);
            Success("Players", Player.Get_Players());
        }

        private void Save_UpdatePlayer()
        {
            int id = Convert.ToInt32(Request["id"]);
            string name = Request["name"];
            string surname = Request["surname"];
            string email = Request["email"];
            string phone = Request["phone"];
            string onlineid = Request["nick"];
            string online = Request["online"];

            if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(email))
            {
                Failure("No name provided");
                return;
            }

            var player = Player.Factory(id, name, surname, email, phone, onlineid, online, default(int?),
                default(int?), null, DateTime.Now, false);

            if (player.Save() > 0)
                Success("Saved player successfully");
            else
                Failure("Unable to save player");
        }

        private static List<Tournament> Get_TournamentsLocal()
        {
            return FifaChamps.API.Logic.Tournament.Get_AllTournaments();
        }
    }

    public class LoginResponse
    {
        public string url;

        public LoginResponse(string u)
        { }
    }
}